<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:nyext="urn:noury-extensions" exclude-result-prefixes="xsl nyext msxsl">
  <xsl:output method="text" encoding="Windows-1252" />

  <xsl:param name="package" />
  <xsl:param name="outdir" />
  <xsl:param name="name" />

  <xsl:include href="functions.xslt" />

	<msxsl:script language="C#" implements-prefix="nyext">
		<![CDATA[
      public static void generateSqlFile(XPathNodeIterator ddlSrc, XPathNodeIterator buildSrc) {
        using (System.CodeDom.Compiler.TempFileCollection tfc=new System.CodeDom.Compiler.TempFileCollection(Environment.GetEnvironmentVariable("TEMP"), false))
        {
          tfc.AddFile(getFullPath(combinePath(tfc.TempDir, "ddl.xml")), false);
          generateXmlFile(ddlSrc, getFullPath(combinePath(tfc.TempDir, "ddl.xml")));

          tfc.AddFile(getFullPath(combinePath(tfc.TempDir, "build.xml")), false);
          generateXmlFile(buildSrc, getFullPath(combinePath(tfc.TempDir, "build.xml")));

          using (System.Diagnostics.Process p=new System.Diagnostics.Process()) {
            p.StartInfo.FileName=getShortPathName(combinePath(Environment.GetEnvironmentVariable("ANT_HOME"), @"bin\ant"));
            p.StartInfo.Arguments="-l ddl.log -f "+getShortPathName(getFullPath(combinePath(tfc.TempDir, "build.xml")));
            p.Start();

            p.WaitForExit();
          }
          
        }
      }
		]]>
	</msxsl:script>

  <xsl:template name="get-ddl-src">
    <database name="{$name}">
      <xsl:for-each select="/manifest/types//table">
        <xsl:sort select="@name" />
        <xsl:copy-of select="." />
      </xsl:for-each>
    </database>
  </xsl:template>
  
  <xsl:template name="get-build-src">
    <project default="database-setup" basedir=".">
    <xsl:attribute name="name">
      <xsl:value-of select="$name" />
    </xsl:attribute>

      <path id="runtime-classpath">
        <fileset dir="lib">
          <include name="**/*.jar"/>
          <include name="**/*.zip"/>
        </fileset>
      </path>
  
      <target name="database-setup" description="Creates the database structure and inserts data into the database">
        <taskdef name="ddlToDatabase" classname="org.apache.ddlutils.task.DdlToDatabaseTask">
          <classpath refid="runtime-classpath"/>
        </taskdef>
  
        <ddlToDatabase>
          <database url="unused" username="unused" password="unused">
            <xsl:variable name="provider" select="/manifest/configuration/sql/@provider" />
            <xsl:attribute name="driverClassName">
              <xsl:choose>
                <xsl:when test="$provider='Axion'">
                  <xsl:text>org.axiondb.jdbc.AxionDriver</xsl:text>
                </xsl:when>
                <xsl:when test="$provider='DB2'">
                  <xsl:text>com.ibm.db2.jcc.DB2Driver</xsl:text>
                </xsl:when>
                <xsl:when test="$provider='Derby'">
                  <xsl:text>org.apache.derby.jdbc.ClientDriver</xsl:text>
                </xsl:when>
                <xsl:when test="$provider='Firebird'">
                  <xsl:text>org.firebirdsql.jdbc.FBDriver</xsl:text>
                </xsl:when>
                <xsl:when test="$provider='HSQLDB'">
                  <xsl:text>org.hsqldb.jdbcDriver</xsl:text>
                </xsl:when>
                <xsl:when test="$provider='Interbase'">
                  <xsl:text>interbase.interclient.Driver</xsl:text>
                </xsl:when>
                <xsl:when test="$provider='MaxDB'">
                  <xsl:text>com.sap.dbtech.jdbc.DriverSapDB</xsl:text>
                </xsl:when>
                <xsl:when test="$provider='McKoi'">
                  <xsl:text>com.mckoi.JDBCDriver</xsl:text>
                </xsl:when>
                <xsl:when test="$provider='MySql'">
                  <xsl:text>com.mysql.jdbc.Driver</xsl:text>
                </xsl:when>
                <xsl:when test="$provider='Oracle'">
                  <xsl:text>oracle.jdbc.driver.OracleDriver</xsl:text>
                </xsl:when>
                <xsl:when test="$provider='PostgreSQL'">
                  <xsl:text>org.postgresql.Driver</xsl:text>
                </xsl:when>
                <xsl:when test="$provider='SQL Server'">
                  <xsl:text>com.microsoft.jdbc.sqlserver.SQLServerDriver</xsl:text>
                </xsl:when>
                <xsl:when test="$provider='Sybase'">
                  <xsl:text>com.sybase.jdbc.SybDriver</xsl:text>
                </xsl:when>
              </xsl:choose>
            </xsl:attribute>
          </database>
          <fileset dir=".">
            <include name="ddl.xml"/>
          </fileset>
  
          <writeSchemaSqlToFile dodrops="false">
            <xsl:attribute name="outputfile">
              <xsl:value-of select="nyext:getFullPath(nyext:combinePath($outdir, concat(nyext:getModelFileName($name), '.sql')))" />
            </xsl:attribute>
          </writeSchemaSqlToFile>
        </ddlToDatabase>
      </target>
  
    </project>
  </xsl:template>
  
  <xsl:template name="generate-model-file">
    <xsl:variable name="ddl">
      <xsl:call-template name="get-ddl-src" />
    </xsl:variable>
    <xsl:variable name="build">
      <xsl:call-template name="get-build-src" />
    </xsl:variable>
    <xsl:value-of select="nyext:generateSqlFile($ddl, $build)" />
  </xsl:template>

    <xsl:template match="value">
      <xsl:text>INSERT INTO </xsl:text>
      <xsl:value-of select="../table/@name" />
      <xsl:text> VALUES ('</xsl:text>
      <xsl:value-of select="@code" />
      <xsl:text>', '</xsl:text>
      <xsl:choose>
        <xsl:when test="string-length(.) &gt; 0">
          <xsl:value-of select="nyext:stringReplace(., &quot;&apos;&quot;, &quot;&apos;&apos;&quot;)" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="@name" />
        </xsl:otherwise>
      </xsl:choose>
      <xsl:text>');
</xsl:text>
    </xsl:template>
  
  <xsl:template name="generate-enum-file">
    <xsl:variable name="src">
      <xsl:text>-- $Id$

</xsl:text>
      <xsl:apply-templates select="/manifest/types/enum/value" />
    </xsl:variable>
    <xsl:value-of select="nyext:generateTextFile($src, nyext:combinePath($outdir, concat($name, '.Enums.sql')))" />
  </xsl:template>
  
  <xsl:template name="generate-user-file">
    <xsl:text>
-- $Id$

</xsl:text>
  </xsl:template>

  <xsl:template match="/">
    <xsl:call-template name="generate-user-file" />
    <xsl:call-template name="generate-enum-file" />
    <xsl:call-template name="generate-model-file" />
  </xsl:template>
</xsl:stylesheet>
